package com.yunhe.authority.controller.company;

import com.yunhe.authority.domain.authority.Menu;
import com.yunhe.authority.service.authority.MenuService;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单Controller
 * @author liuronglei
 */
@Api(value = "menu",description = "企业菜单维护", tags = {"Authority_CompanyMenu"})
@RestController
@RequestMapping("/companyMenus")
public class CompanyMenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 根据企业ID查询菜单列表
     */
    @ApiOperation("根据企业ID查询菜单列表")
    @RequestMapping(value = "/searchByCompanyId", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业ID", required = true, dataType = "Long", paramType = "query")
    })
    public ResultObject<List<Menu>> searchByCompanyId(@RequestParam(value = "companyId", required = false) Long companyId) {
        List<Menu> results = menuService.findMenusByCompanyId(companyId);
        return new ResultObject<>(results);
    }

    /**
     * 根据企业ID再加上角色id查询菜单列表
     * ok
     */
    @ApiOperation("根据企业ID再加上角色id查询菜单列表")
    @RequestMapping(value = "/findMenusByCompanyIdAndRoleId", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Long", paramType = "query")
    })
    public ResultObject<List<Menu>> findMenusByCompanyIdAndRoleId(@RequestParam(value = "companyId", required = true) Long companyId,
                                                                  @RequestParam(value = "roleId", required = true) Long roleId)
                                                            throws ArgumentErrorException {
        List<Menu> results = menuService.findMenusByCompanyIdAndRoleId(companyId, roleId);
        return new ResultObject<>(results);
    }

    /**
     * 企业绑定/解绑菜单
     * ok
     */
    @ApiOperation("企业绑定/解绑菜单")
    @RequestMapping(value = "/menusBindCompany", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "菜单ID列表", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "companyId", value = "企业ID", required = true, dataType = "Long", paramType = "query")
    })
    public ResultObject<String> menusBindCompany(@RequestParam(value = "ids", required = true) String ids,
                                                 @RequestParam(value = "companyId", required = true) Long companyId,
                                                 @RequestParam(value = "systemMenuCategory", required = true) String systemMenuCategory) {
        menuService.menusBindCompany(ids, companyId, systemMenuCategory);
        return new ResultObject<>("success");
    }
}